import base64
import os
import uuid

import requests

from common import constants
from common.util import Util

# 测试环境
url = "http://192.168.8.244:4000/rpc"
# 本地环境
# url = "http://localhost:4000/rpc"

app_key = "APP_KEY",
app_secret = "APP_SECRET",
open_conversation_id = "OPEN_CONVERSATION_ID",
robot_code = "ROBOT_CODE"


def sent_custom_text_msg():
    payload = {
        "jsonrpc": "2.0",
        "method": "sent_custom_text_msg",
        "params": {
            "msg": "客户端测试消息",
            "context": {
                "app_key": app_key,
                "app_secret": app_secret,
                "open_conversation_id": open_conversation_id,
                "robot_code": robot_code
            }
        },
        "id": str(uuid.uuid4())
    }
    # 发送请求
    response = requests.post(url, json=payload).json()
    print(response)


def sent_markdown_msg():
    msg = "### 【任务领取通知】" \
          "\n\n尊敬的清洁同事：" \
          "\n\n您好！新的客房清洁任务已发布，请及时点击下方链接领取您的任务：" \
          "\n\n[立即领取任务](https://www.baidu.com/)" \
          "\n\n如需协助，请联系主管张经理：138-XXXX-XXXX。感谢您的配合！" \
          "\n\n<font color='red'>**酒店管理部**</font>"
    payload = {
        "jsonrpc": "2.0",
        "method": "sent_markdown_msg",
        "params": {
            "title": "文档测试",
            "msg": msg,
            "context": {
                "app_key": app_key,
                "app_secret": app_secret,
                "open_conversation_id": open_conversation_id,
                "robot_code": robot_code
            }
        },
        "id": str(uuid.uuid4())
    }
    # 发送请求
    response = requests.post(url, json=payload).json()
    print(response)


def sent_image_link():
    object_name = "screenshot/000.png"
    image_url = get_file_public_network_address("crawler", object_name)
    payload = {
        "jsonrpc": "2.0",
        "method": "sent_image_link",
        "params": {
            "image_url": image_url
        },
        "id": str(uuid.uuid4())
    }
    # 发送请求
    response = requests.post(url, json=payload).json()
    print(response)


def sent_image_msg():
    object_name = "screenshot/000.png"
    image_url = get_file_public_network_address("crawler", object_name)
    payload = {
        "jsonrpc": "2.0",
        "method": "sent_image_msg",
        "params": {
            "title": "异常快照",
            "image_url": image_url
        },
        "id": str(uuid.uuid4())
    }
    # 发送请求
    response = requests.post(url, json=payload).json()
    print(response)


def sent_file_msg():
    # 使用本接口最好关闭VPN代理
    screenshot_name = "000.jpg"
    screenshot_dir = Util.unified_static_path(constants.FILES_SAVE_DIRECTORY)
    screenshot_path = os.path.join(screenshot_dir, screenshot_name)
    with open(screenshot_path, 'rb') as file:
        file_content = base64.b64encode(file.read()).decode('utf-8')
    payload = {
        "jsonrpc": "2.0",
        "method": "sent_file_msg",
        "params": {
            "file_name": screenshot_name,
            "file_content": file_content,
            "context": {
                "app_key": app_key,
                "app_secret": app_secret,
                "open_conversation_id": open_conversation_id,
                "robot_code": robot_code
            }
        },
        "id": str(uuid.uuid4())
    }
    # 发送请求
    response = requests.post(url, json=payload).json()
    print(response)


def sent_error_msg():
    payload = {
        "jsonrpc": "2.0",
        "method": "sent_error_msg",
        "params": {
            "program_name": "美团-98-1"
        },
        "id": str(uuid.uuid4())
    }
    # 发送请求
    response = requests.post(url, json=payload).json()
    print(response)


def sent_cease_msg():
    payload = {
        "jsonrpc": "2.0",
        "method": "sent_cease_msg",
        "params": {
            "program_name": "携程-98-1"
        },
        "id": str(uuid.uuid4())
    }
    # 发送请求
    response = requests.post(url, json=payload).json()
    print(response)


def send_sms():
    payload = {
        "jsonrpc": "2.0",
        "method": "send_sms",
        "params": {
            "template_code": "SMS_154950909",
            "params": {"code": "5000"},
            "context": {
                "endpoint": "dysmsapi.aliyuncs.com",
                "sign_name": "阿里云短信测试",
                "phone_numbers": [
                    "110",
                    "120"
                ]
            }
        },
        "id": str(uuid.uuid4())
    }
    # 发送请求
    response = requests.post(url, json=payload).json()
    print(response)


if __name__ == '__main__':
    sent_markdown_msg()


def get_file_public_network_address(bucket_name, object_name):
    """
    获取文件的访问地址
    :param bucket_name: bucket名称
    :param object_name: bucket中的对象名称
    """
    access_protocol = "http"
    access_host = "192.168.8.242"
    access_port = 9980
    network_address = f"{access_host}:{access_port}"
    object_path = f"{bucket_name}/{object_name}"
    return f"{access_protocol}://{network_address}/{object_path}"
